<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\admin;

use suifeng\utils\HtmlUtil;

final class AdminTable
{
//     public $array = array();

//     public function __construct($array = null)
//     {
//         $this->array = $array;
//     }

//     public function renderTable($options)
//     {
//         $controller = isset($options['controller']) ? $options['controller'] : null;
//         $form_url = isset($options['form_url']) ? $options['form_url'] : request()->url();
//         $page_url = isset($options['page_url']) ? $options['page_url'] : $controller->url('index', array('page' => '[page]'));
//         $key_field = isset($options['key_field']) ? $options['key_field'] : 'id';
//         $selectable = isset($options['selectable']) ? $options['selectable'] : true;
//         $columns = isset($options['columns']) ? $options['columns'] : array();
//         $actions = isset($options['actions']) ? $options['actions'] : array();
//         $pagination = isset($options['pagination']) ? $options['pagination'] : array();
//         unset($options['controller'], $options['form_url'], $options['page_url'], $options['key_field'], $options['selectable'], $options['columns'], $options['actions'], $options['pagination']);

//         $selected = isset($pagination['selected']) ? $pagination['selected'] : array();

//         $form_attributes['method'] = 'get';
//         $form_attributes['action'] = $form_url;
//         $html = HtmlUtil::render_start_tag('form', $form_attributes);

//         $table_attributes = $options;
//         $table_attributes['class'] = 'table table-bordered table-striped table-hover';
//         $html .= HtmlUtil::render_start_tag('table', $table_attributes);
//         $html .= HtmlUtil::render_start_tag('thead');
//         $html .= HtmlUtil::render_start_tag('tr');

//         if ($selectable) {
//             $html .= HtmlUtil::render_tag('th', array('class' => 'list-ids', 'width' => '30'), $this->renderHeaderSelect());
//         }

//         foreach ($columns as $field => $config) {
//             if (isset($config['title'])) {
//                 $title = $config['title'];
//                 unset($config['title']);
//             } else {
//                 $title = strtoupper($field);
//             }

//             if (isset($config['header'])) {
//                 $header_attributes = $config['header'];
//                 unset($config['header']);
//             } else {
//                 $header_attributes = null;
//             }

//             $html .= HtmlUtil::render_tag('th', $header_attributes, $this->renderHeaderText($title));
//         }

//         if ($actions) {
//             $html .= HtmlUtil::render_tag('th', null, $this->renderHeaderText('操作'));
//         }

//         $html .= HtmlUtil::render_end_tag('tr');
//         $html .= HtmlUtil::render_end_tag('thead');
//         $html .= HtmlUtil::render_start_tag('tbody');

//         if ($this->array) {
//             foreach ($this->array as $row) {
//                 $html .= HtmlUtil::render_start_tag('tr');

//                 if ($selectable) {
//                     $html .= HtmlUtil::render_tag('td', null, $this->renderColumnSelect($row, $key_field, array('selected' => $selected)));
//                 }

//                 foreach ($columns as $field => $config) {
//                     if (isset($config['column'])) {
//                         $column_attributes = $config['column'];
//                         unset($config['column']);
//                     } else {
//                         $column_attributes = array();
//                     }

//                     $html .= HtmlUtil::render_tag('td', $column_attributes, $this->renderColumnText($row, $field, $config));
//                 }

//                 if ($actions) {
//                     $html .= HtmlUtil::render_start_tag('td', array('class' => 'actions'));

//                     foreach ($actions as $action) {
//                         if (isset($action['title'])) {
//                             $text = $action['title'];
//                             unset($action['title']);
//                         } else {
//                             $text = null;
//                         }

//                         if (isset($action['icon'])) {
//                             $icon = $action['icon'];
//                             unset($action['icon']);
//                         } else {
//                             $icon = null;
//                         }

//                         if (isset($action['class'])) {
//                             $class = $action['class'];
//                             unset($action['class']);
//                         } else {
//                             $class = null;
//                         }

//                         if ($action['url']) {
//                             $key_value = $this->_getValue($row, $key_field);
//                             $action['url'] = str_replace(urlencode('[id]'), $key_value, $action['url']);
//                         }

//                         $html .= $this->renderAction($text, $icon, $class, $action);
//                     }

//                     $html .= HtmlUtil::render_end_tag('td');
//                 }

//                 $html .= HtmlUtil::render_end_tag('tr');
//             }
//         } else {
//             $html .= HtmlUtil::render_start_tag('tr', array('class' => 'data-empty'));
//             $html .= HtmlUtil::render_start_tag('td', array('class' => 'text-center empty-info', 'colspan' => '50'));
//             $html .= HtmlUtil::render_tag('i', array('class' => 'fa fa-database'));
//             $html .= HtmlUtil::render_tag('span', null, '暂时没有数据');
//             $html .= HtmlUtil::render_end_tag('td');
//             $html .= HtmlUtil::render_end_tag('tr');
//         }

//         $html .= HtmlUtil::render_end_tag('tbody');
//         $html .= HtmlUtil::render_end_tag('table');
//         $html .= HtmlUtil::render_end_tag('form');

//         if ($this->array && $pagination) {
//             $page_object = new \suifeng\builder\Pagination();
//             $page_object->totalRows = $pagination['rows_count'];
//             $page_object->listRows = $pagination['page_size'];
//             $page_object->nowPage = $pagination['page_num'];
//             $page_object->url = $page_url;

//             $html .= HtmlUtil::render_start_tag('div', array('class' => 'row'));

//             $html .= HtmlUtil::render_start_tag('div', array('class' => 'col-sm-5'));
//             $html .= HtmlUtil::render_start_tag('div', array('class' => 'pagination-info'));
//             $html .= $page_object->show_info();
//             $html .= HtmlUtil::render_end_tag('div');
//             $html .= HtmlUtil::render_end_tag('div');

//             $html .= HtmlUtil::render_start_tag('div', array('class' => 'col-sm-7'));
//             $html .= HtmlUtil::render_start_tag('ul', array('class' => 'pagination'));
//             $html .= $page_object->show_pages();
//             $html .= HtmlUtil::render_end_tag('ul');
//             $html .= HtmlUtil::render_end_tag('div');

//             $html .= HtmlUtil::render_end_tag('div');
//         }

//         return $html;
//     }

//     public function renderHeaderSelect($options = array())
//     {
//         $attributes = $options;
//         $attributes['type'] = 'checkbox';
//         $attributes['class'] = 'check-all';
//         return HtmlUtil::render_tag('input', $attributes);
//     }

//     public function renderHeaderText($text, $options = array())
//     {
//         if (empty($options)) {
//             return $text;
//         }

//         $attributes = $options;
//         return HtmlUtil::render_tag('span', $attributes, $text);
//     }

//     public function renderColumnText($row, $field, $options = array())
//     {
//         if (isset($options['list'])) {
//             $list = $options['list'];
//             unset($options['list']);
//         } else {
//             $list = array();
//         }

//         $value = $this->_getValue($row, $field);

//         if (array_key_exists($value, $list)) {
//             $display = $list[$value];

//             if (is_array($display)) {
//                 $text = array_shift($display);
//                 $class = array_shift($display);

//                 $attributes = $options;
//                 $attributes['class'] = $class;
//                 $display = HtmlUtil::render_tag('span', $attributes, $text);
//             }
//         } else {
//             $display = $value;
//         }

//         return $display;
//     }

//     public function renderColumnStatus($row, $field, $options = array())
//     {
//         $options['list'] = array(
//             0 => array('无效', 'label label-danger'),
//             1 => array('有效', 'label label-success'),
//         );
//         return $this->renderColumnText($row, $field, $options);
//     }

//     public function renderColumnSelect($row, $field, $options = array())
//     {
//         if (isset($options['selected'])) {
//             $selected = $options['selected'];
//             unset($options['selected']);
//         } else {
//             $selected = array();
//         }

//         $value = $this->_getValue($row, $field);
//         $checked = in_array($value, $selected);

//         $attributes = $options;
//         $attributes['type'] = 'checkbox';
//         $attributes['class'] = 'ids';
//         $attributes['name'] = 'ids[]';
//         $attributes['value'] = $value;
//         if ($checked) {
//             $attributes['checked'] = 'checked';
//         }
//         return HtmlUtil::render_tag('input', $attributes);
//     }

//     private function _getValue($row, $field)
//     {
//         if (isset($row[$field])) {
//             return $row[$field];
//         } else {
//             return null;
//         }
//     }

//     public function renderAction($text, $icon, $class, $options = array())
//     {
//         $icon_html = $icon ? HtmlUtil::render_tag('i', array('class' => $icon)) : null;

//         $attributes = $options;
//         $attributes['type'] = 'button';
//         $attributes['class'] = $class;
//         $attributes['title'] = $text;
//         return HtmlUtil::render_tag('button', $attributes, $icon_html);
//     }



    public function render_header_select($options = array())
    {
        $attributes['type'] = 'checkbox';
        $attributes['class'] = 'check-all';
        $attributes = array_merge($attributes, $options);
        return HtmlUtil::render_tag('input', $attributes);
    }

    public function render_header_text($text, $options = array())
    {
        if (empty($options)) {
            return $text;
        }

        $attributes = $options;
        return HtmlUtil::render_tag('span', $attributes, $text);
    }

    public function render_column_select($value, $options = array())
    {
        if (isset($options['selected'])) {
            $selected = $options['selected'];
            unset($options['selected']);
        } else {
            $selected = array();
        }

        $attributes['type'] = 'checkbox';
        $attributes['class'] = 'ids';
        $attributes['name'] = 'ids[]';
        $attributes['value'] = $value;
        in_array($value, $selected) && $attributes['checked'] = 'checked';
        $attributes = array_merge($attributes, $options);
        return HtmlUtil::render_tag('input', $attributes);
    }

    public function render_column_text($value, $options = array())
    {
        if (isset($options['list'])) {
            $list = (array) $options['list'];
            unset($options['list']);
        } else {
            $list = array();
        }

        if (isset($options['link'])) {
            $link = $options['link'];
            unset($options['link']);
        } else {
            $link = '';
        }

        if ($list) {
            if (array_key_exists($value, $list)) {
                $display = $list[$value];

                if (is_array($display)) {
                    $text = array_shift($display);
                    $class = array_shift($display);

                    $attributes = $options;
                    $attributes['class'] = $class;
                    $display = HtmlUtil::render_tag('span', $attributes, $text);
                }
            } else {
                $display = '';
            }
        } else {
            $display = $value;
        }

        if ($link) {
            $attributes = $options;
            $attributes['href'] = $link;
            $display = HtmlUtil::render_tag('a', $attributes, $display);
        }

        return $display;
    }

    public function render_column_url($value, $options = array())
    {
        if ($value) {
            $attributes['href'] = $value;
            $attributes['class'] = 'url';
            $attributes['target'] = '_blank';
            $attributes = array_merge($attributes, $options);
            return HtmlUtil::render_tag('a', $attributes, $value);
        } else {
            return '';
        }
    }

    public function render_column_status($value, $options = array())
    {
        $options['list'] = array(
            0 => array('无效', 'label label-danger'),
            1 => array('有效', 'label label-success'),
        );
        return $this->render_column_text($value, $options);
    }

    public function render_column_image($value, $options = array())
    {
        $url = model('admin/upload')->get_url($value);
        if ($url) {
            $attributes['src'] = $url;
            $attributes['class'] = 'thumb';
            $attributes = array_merge($attributes, $options);
            return HtmlUtil::render_tag('img', $attributes);
        } else {
            return '';
        }
    }

    public function render_column_date_ts($value, $options = array())
    {
        if ($value) {
            $display = sf_format_date($value);
            $attributes['class'] = 'date_ts';
            $attributes = array_merge($attributes, $options);
            return HtmlUtil::render_tag('span', $attributes, $display);
        } else {
            return '';
        }
    }

    public function render_action($text, $icon, $class, $options = array())
    {
        $icon_html = $icon ? HtmlUtil::render_tag('i', array('class' => $icon)) : null;

        $attributes['type'] = 'button';
        $attributes['class'] = $class;
        $attributes['title'] = $text;
        $attributes = array_merge($attributes, $options);
        return HtmlUtil::render_tag('button', $attributes, $icon_html);
    }

    public function render_empty_row($options = array())
    {
        $html = '';
        $html .= HtmlUtil::render_start_tag('tr', array('class' => 'data-empty'));
        $html .= HtmlUtil::render_start_tag('td', array('class' => 'text-center empty-info', 'colspan' => '50'));
        $html .= HtmlUtil::render_tag('i', array('class' => 'fa fa-database'));
        $html .= HtmlUtil::render_tag('span', null, '暂时没有数据');
        $html .= HtmlUtil::render_end_tag('td');
        $html .= HtmlUtil::render_end_tag('tr');
        return $html;
    }

    public function render_pagination($pagination, $page_url)
    {
        $page_object = new \suifeng\admin\Pagination();
        $page_object->totalRows = $pagination['rows_count'];
        $page_object->listRows = $pagination['page_size'];
        $page_object->nowPage = $pagination['page_num'];
        $page_object->url = $page_url;

        $html = '';
        $html .= HtmlUtil::render_start_tag('div', array('class' => 'row'));

        $html .= HtmlUtil::render_start_tag('div', array('class' => 'col-sm-5'));
        $html .= HtmlUtil::render_start_tag('div', array('class' => 'pagination-info'));
        $html .= $page_object->show_info();
        $html .= HtmlUtil::render_end_tag('div');
        $html .= HtmlUtil::render_end_tag('div');

        $html .= HtmlUtil::render_start_tag('div', array('class' => 'col-sm-7'));
        $html .= HtmlUtil::render_start_tag('ul', array('class' => 'pagination'));
        $html .= $page_object->show_pages();
        $html .= HtmlUtil::render_end_tag('ul');
        $html .= HtmlUtil::render_end_tag('div');

        $html .= HtmlUtil::render_end_tag('div');
        return $html;
    }
}